package com.qf.ssm.Cache;

import com.qf.ssm.event.CacheEvent;
import com.qf.ssm.mapper.StudentMapper;
import com.qf.ssm.pojo.TbStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王博康
 * @version 1.0
 * @project 07_ssm
 * @description: TODO
 * @date 2022/3/11 20:45 星期五
 */
@Component
public class SceneCache {
    private List<TbStudent> data=new ArrayList<>();

    private StudentMapper studentMapper;
    @Autowired
    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }
    @PostConstruct
    private void init(){
        List<TbStudent> studentList = studentMapper.findAll();
        data.clear();
        data.addAll(studentList);
    }

    public List<TbStudent> getData(){
        return this.data;
    }

    @EventListener
    public void overinit(CacheEvent cacheEvent){
        System.out.println("数据变化了...");
        init();
        System.out.println("重新加载完毕");
    }

}
